package com.elite;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;  
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;  
import org.apache.commons.net.ftp.FTPReply;  
import org.apache.commons.net.ftp.FTP;  

import java.net.URLEncoder;

public class test {
	private static final Log log=LogFactory.getLog(test.class);
	public static void main(String args[]) throws SocketException, IOException{
		FTPClient ftp=GetFtp("127.0.0.1",21,"tony","");
		String a="我.txt";
//		String a="q.txt";
		a=URLEncoder.encode(a, "UTF-8");
		FTPFile[] aa=ftp.listFiles("a/");
		for(int i=0;i<aa.length;i++){
			System.out.println(aa[i].getName());
//			String name=aa[i].getName();
			String name=new String(aa[i].getName().getBytes("gbk"),"iso8859-1");
			//System.out.println("----------"+name);
			System.out.println(ftp.rename("a/"+name,"b/"+name));
			System.out.println(name);
			log.info("a:"+new String(name.getBytes("iso8859-1"),"gbk"));
		}
//		boolean aaa=ftp.rename("a/"+a,"b/"+a);
		
		log.info("a:"+aa);
//		boolean a=ftp.makeDirectory("bbb");
//		System.out.println(a);
		
//		getFile(ftp);
//		ftp.logout();
//		ftp.disconnect();
		
	}
	
	public static FTPClient GetFtp(String host,int port,String username,String password){
		FTPClient ftp;
		ftp = new FTPClient(); 
		try{
			ftp.connect(host, port); 
			ftp.setControlEncoding("GBK");
			FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
			conf.setServerLanguageCode("zh");
			ftp.login(username, password);
		}catch(Exception e){
			log.info("FTP连接失败:"+e);
		}
		return ftp;
		
	}
    public static void getFile(FTPClient ftp) throws UnsupportedEncodingException{
    	FTPFile[] ff=null;
    	try{
    		ff=ftp.listFiles();
    		
    	}catch(Exception e){
    		log.info("错误:"+e);
    	}
    	String filename="";
    	String filepath="";
    	if(ff!=null){
    		log.info("文件个数:"+ff.length);
    		for(int i=0;i<ff.length;i++){
    			filename=ff[i].getName();
//    			log.info("dddddddddd:"+filename);
    			try{
//        			boolean a=ftp.rename("/ftp/aaa/"+ff[i].getName(),"/ftp/aaa/bbb/"+ff[i].getName());
//        			log.info("a:"+a);
    			}catch(Exception e){
    				log.info("e:"+e);
    			}
//    			filename=new String(filename.getBytes("GBK"), "ISO-8859-1");
    			log.info("文件名:"+filename);
    		}
    	}else{
    		log.info("没有文件");
    	}
    }
    public static boolean makeDirectory(String dir){
    	boolean flag = true; 
    	return flag;
    }
    
    
}
